<template>
  <!-- 修改当前时间段 -->
    <el-dialog title="修改当前时间段" :visible.sync="openContractCurrentDateEdit" width="600px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
      <el-form ref="formParams" :model="formParams" :rules="rules" label-width="150px" v-show="showSearch" :inline="true">
         <el-form-item label="修改时间段" prop="sdate" :rules="rules.sdate && rules.edate">
            <el-date-picker
              v-model="date1"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeDate1">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="不可选择时间段" prop="date2">
            <el-date-picker
              v-model="date2"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :disabled="true">
            </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit('formParams')" v-hasPermi="['zhcloud:sfbz:modify']">修 改</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { contractCurrentDateModify, getContractNotOptionalDate  }  from "@/api/storage/contractFeeStandard";

export default {
  name: "ContractCurrentDateEdit",
  data() {
    return {
      showSearch:true,
      //弹窗状态
      openContractCurrentDateEdit: false,
      //不可创建日期
      date1:[],
      //最新版次日期
      date2:[],
      //参数
      queryParams:{
        csuid:"",
        cx:"",
      },
      //表单参数
      formParams: {
      	csuid: "",
        cx: "",
        sdate: "",
        edate: "",
      },
      rules: {
        sdate: [
          { required: true, message: "修改时间段不能为空", trigger: "blur" }
        ],
        edate: [
          { required: true, message: "修改时间段不能为空", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    getDetail() {
        this.$modal.loading("数据加载数据，请稍等...")
        getContractNotOptionalDate(this.queryParams).then(response => {
           const data = response.data[0]
           if(data.sm && data.em) {
               this.date2 = [data.sm, data.em]
           }
           this.$modal.closeLoading()
        }).catch(()=> {
            this.$modal.closeLoading()
        })
    },
    editContractCurrentDate() {
      this.$modal.loading("数据正在修改中，请稍等...")
      contractCurrentDateModify(this.formParams).then(response => {
        this.$modal.closeLoading()
        this.$emit('contractCurrentDateEdited', this.date1)
        this.close()
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },
    /**添加合同 */
    handleEdit(formName) {
      this.$refs[formName].validate((valid, data) => {
        if(valid) {
             this.$modal.confirm("是否修改？").then(() => {
               this.editContractCurrentDate()
            }).catch(() => {})
        } else {
          for(let i in data) {
            const item = data[i]
            for(let j in item) {
              const message = item[j].message
              return this.$modal.msgError(message)
            }
          }
          return false
        }
      })
    },
    /**修改时间段改变 */
    changeDate1() {
        this.formParams.sdate = this.date1?this.date1[0]:""
        this.formParams.edate = this.date1?this.date1[1]:""
    },
    /**改变窗口状态 */
    changeContractCurrentDateEdit() {
      this.openContractCurrentDateEdit = !this.openContractCurrentDateEdit;
    },
    /**重置数据 */
    reset(vm) {
      //Object.assign(vm.$data, vm.$options.data())
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      done()
    },
    /**关闭弹窗 */
    close() {
      this.changeContractCurrentDateEdit()
      this.reset(this)
    },
    /**打开弹窗 */
    open(data) {
      this.changeContractCurrentDateEdit()
      this.formParams = {
          ...this.formParams,
          csuid:data.uid,
          cx:data.cx,
          sdate:data.sdate,
          edate:data.edate
      }
      this.queryParams = {
          csuid:data.uid, 
          cx:data.cx
      }
      this.date1 = [data.sdate, data.edate]
      this.getDetail()
    }
  }
};
</script>
